# -*- coding:UTF-8 -*-
from django.db.models import Q
from cmdb.models import NetworkInfo

import string

def network_info(start,end,serverRoomId,searchStr):
    count = 0
    netRes = {}
    if searchStr == '':
        if serverRoomId == 0:
            count=NetworkInfo.objects.count()
            netRes = NetworkInfo.objects.order_by('-id')[start:end]
        else:
            count=NetworkInfo.objects.filter(server_room_id=serverRoomId).count()
            netRes = NetworkInfo.objects.filter(server_room_id=serverRoomId).order_by('-id')[start:end]
    else:
        if serverRoomId == 0:
            count=NetworkInfo.objects.filter(Q(asset_number__icontains=searchStr) | Q(manger_ip__icontains=searchStr) | Q(serial_num__icontains=searchStr)).count()
            netRes = NetworkInfo.objects.filter(Q(asset_number__icontains=searchStr) | Q(manger_ip__icontains=searchStr) | Q(serial_num__icontains=searchStr)).order_by('-id')[start:end]
        else:
            count=NetworkInfo.objects.filter(Q(server_room_id=serverRoomId),Q(asset_number__icontains=searchStr) | Q(manger_ip__icontains=searchStr) | Q(serial_num__icontains=searchStr)).count()
            netRes = NetworkInfo.objects.filter(Q(server_room_id=serverRoomId),Q(asset_number__icontains=searchStr) | Q(manger_ip__icontains=searchStr) | Q(serial_num__icontains=searchStr)).order_by('-id')[start:end]
    return {'count':count,'network_info_list':netRes}

def add_network_info(equipment_no, equipment_model, equipment_type, equipment_ver,
                     hostname, manger_ip,asset_number,server_room_id, cabinet,
                     start_cabinet, end_cabinet,serial_num,purchase_time,
                     maint_end_time,comment, operator):
    equipment_no = 'NE00001'
    eqNum=NetworkInfo.objects.count()
    if eqNum > 0:
        netNoRes=NetworkInfo.objects.latest('equipment_no')
        if netNoRes:
            tmpNo=str(int(netNoRes.equipment_no[2:]) + 1)
            equipment_no = 'NE' + string.rjust(tmpNo,5,'0')
    occupy_u = abs(int(end_cabinet)-int(start_cabinet))
    netInfo = NetworkInfo(equipment_no=equipment_no, equipment_model=equipment_model, equipment_type=equipment_type, equipment_ver=equipment_ver,
                              hostname=hostname, manger_ip=manger_ip,asset_number=asset_number,server_room_id=server_room_id, cabinet=cabinet,
                              start_cabinet=start_cabinet, end_cabinet=end_cabinet, occupy_u=occupy_u,serial_num=serial_num,purchase_time=purchase_time,
                              maint_end_time=maint_end_time,comment=comment, operator=operator)
    netInfo.save()

def get_network_info(equipment_no):
    netInfo=NetworkInfo.objects.get(equipment_no=equipment_no)
    return netInfo

def edit_network_info(equipment_no, equipment_model, equipment_type, equipment_ver,
                     hostname, manger_ip,asset_number,server_room_id, cabinet,
                     start_cabinet, end_cabinet,serial_num,purchase_time,
                     maint_end_time,comment, operator):
    netInfo = NetworkInfo.objects.get(equipment_no=equipment_no)
    netInfo.equipment_model = equipment_model
    netInfo.equipment_type = equipment_type
    netInfo.equipment_ver = equipment_ver
    netInfo.hostname = hostname
    netInfo.manger_ip = manger_ip
    netInfo.asset_number = asset_number
    netInfo.server_room_id = server_room_id
    netInfo.cabinet = cabinet
    netInfo.start_cabinet = start_cabinet
    netInfo.end_cabinet = end_cabinet
    netInfo.occupy_u = abs(int(end_cabinet)-int(start_cabinet))
    netInfo.serial_num = serial_num
    netInfo.purchase_time = purchase_time
    netInfo.maint_end_time = maint_end_time
    netInfo.comment = comment
    netInfo.operator = operator
    netInfo.save()

def del_network_info(equipment_no):
    NetworkInfo.objects.get(equipment_no=equipment_no).delete()


def exist_serial_network_info(serial_num):
    count = NetworkInfo.objects.filter(serial_num=serial_num).count()
    if count > 0:
        return True
    return False

def exclude_serial_network_info(serial_num, id):
    count = NetworkInfo.objects.filter(serial_num=serial_num).exclude(id=id).count()
    if count > 0:
        return True
    return False